/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.odb.bzk3.midp;

import java.util.Vector;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;
import javax.microedition.m3g.Background;
import javax.microedition.m3g.Camera;
import javax.microedition.m3g.Graphics3D;
import javax.microedition.m3g.Mesh;
import javax.microedition.m3g.World;

import br.odb.bzk3.geometry.Actor;
import br.odb.bzk3.geometry.Vec3;

/**
 * 
 * @author daniel
 */
public class M3GRenderer extends Canvas {

	/** Graphics singleton used for rendering. */
	Graphics3D iGraphics3d;
	// -----------------------
	World iWorld;
	Vec3 iOrigin;
	public int[] Keymaps;
	Camera iCamera3D;
	public Background background = new Background();
	public Vector shapes;

	public M3GRenderer() {
		shapes = new Vector();
		iOrigin = new Vec3();
		iGraphics3d = Graphics3D.getInstance();
		iGraphics3d.setDepthRange(0.1f, 0.9f);

		iWorld = new World();
		iCamera3D = new Camera();
		iCamera3D.setPerspective(45.0f, 1.0f, 0.1f, 255.0f);

		background.setColor(0xFF0000);
		iWorld.setBackground(background);
		iWorld.addChild(SquareFactory.MakeXY(-128.0f, -128.0f, 128.0f, 128.0f,
				128.0f, 0xFF0000));
		iWorld.addChild(SquareFactory.MakeXY(-128.0f, -128.0f, 128.0f, 128.0f,
				-128.0f, 0xAA0000));

		iWorld.addChild(SquareFactory.MakeXZ(-128.0f, -128.0f, 128.0f, 128.0f,
				128.0f, 0x990000));
		iWorld.addChild(SquareFactory.MakeXZ(-128.0f, -128.0f, 128.0f, 128.0f,
				-128.0f, 0x660000));

		iWorld.addChild(SquareFactory.MakeYZ(-128.0f, -128.0f, 128.0f, 128.0f,
				128.0f, 0x330000));
		iWorld.addChild(SquareFactory.MakeYZ(-128.0f, -128.0f, 128.0f, 128.0f,
				-128.0f, 0110000));
		iWorld.addChild(iCamera3D);

		Reset();
		Keymaps = new int[10];
		iWorld.setActiveCamera(iCamera3D);
		iCamera3D.setPickingEnable(false);
		iWorld.setPickingEnable(false);
	}

	public static int GetDirection(int aAngle) {
		while (aAngle < 0) {
			aAngle += 16;
		}
		aAngle = (aAngle * 360) / 16;
		int toReturn = (int) (((aAngle + 45) / 90) % 4);
		// if (toReturn == 0)
		// {
		// toReturn = 2;
		// }
		// else if (toReturn == 2)
		// {
		// toReturn = 0;
		// }
		return toReturn;

	}

	protected void paint(Graphics graphics) {
		iGraphics3d.bindTarget(graphics);
		iGraphics3d.render(iWorld);
		iGraphics3d.releaseTarget();
		// graphics.setColor(255,255,255);
		// graphics.fillRect(0,0, getWidth(), 25);
		// graphics.setColor(0,0,0);
		// graphics.drawString("position:" + iOrigin.toString(), 0, 0,0);
		Triangle t;
		for (int c = 0; c < shapes.size(); ++c) {
			t = (Triangle) shapes.elementAt(c);
			t.draw(graphics);
		}
		// graphics.setColor( 0xFF0000 );
		// graphics.fillTriangle( 0, 0, 240, 0, 120, 320 );

	}

	public void SetCamera(Actor actor) {
		// Reset();
		Translate(actor.iStartPoint.iX, actor.iStartPoint.iY,
				actor.iStartPoint.iZ);
		Rotate(actor.iAngle);
	}

	void Reset() {
		iCamera3D.translate(iOrigin.iX, iOrigin.iY, iOrigin.iZ);
		// iCamera3D.setOrientation(0.0f, 0.0f, 1.0f, 0.0f);
	}

	void Translate(int x, int y, int z) {
		// iOrigin.iX = (int) (x);
		// iOrigin.iY = (int) (y);
		// iOrigin.iZ = (int) (z);
		iCamera3D.translate(x, y, z);
	}

	void Rotate(int angle) {
		iCamera3D.setOrientation(angle, 0.0f, 1.0f, 0.0f);
	}

	protected void keyPressed(int arg0) {
		if (arg0 == M3GRenderer.KEY_NUM2 || arg0 == M3GRenderer.UP) {
			Keymaps[0] = 1;
		}
		if (arg0 == M3GRenderer.KEY_NUM6 || arg0 == M3GRenderer.RIGHT) {
			Keymaps[1] = 1;
		}
		if (arg0 == M3GRenderer.KEY_NUM8 || arg0 == M3GRenderer.DOWN) {
			Keymaps[2] = 1;
		}
		if (arg0 == M3GRenderer.KEY_NUM4 || arg0 == M3GRenderer.LEFT) {
			Keymaps[3] = 1;
		}
		if (arg0 == M3GRenderer.KEY_NUM5 || arg0 == M3GRenderer.FIRE) {
			Keymaps[4] = 1;
		}
		if (arg0 == M3GRenderer.KEY_NUM0) {
			Keymaps[5] = 1;
		}

		if (arg0 == M3GRenderer.KEY_POUND) {
			Keymaps[8] = 1;
		}
		if (arg0 == M3GRenderer.KEY_STAR) {
			Keymaps[9] = 1;
		}

		switch (getGameAction(arg0)) {
		case RIGHT:
			Keymaps[1] = 1;
			break;
		case LEFT:
			Keymaps[3] = 1;
			break;
		case UP:
			Keymaps[0] = 1;
			break;
		case DOWN:
			Keymaps[2] = 1;
			break;
		case FIRE:
			Keymaps[4] = 1;
			break;
		}

	}

	protected void keyReleased(int arg0) {
		if (arg0 == M3GRenderer.KEY_NUM2 || arg0 == M3GRenderer.UP) {
			Keymaps[0] = 0;
		}
		if (arg0 == M3GRenderer.KEY_NUM6 || arg0 == M3GRenderer.RIGHT) {
			Keymaps[1] = 0;
		}
		if (arg0 == M3GRenderer.KEY_NUM8 || arg0 == M3GRenderer.DOWN) {
			Keymaps[2] = 0;
		}
		if (arg0 == M3GRenderer.KEY_NUM4 || arg0 == M3GRenderer.LEFT) {
			Keymaps[3] = 0;
		}
		if (arg0 == M3GRenderer.KEY_NUM5 || arg0 == M3GRenderer.FIRE) {
			Keymaps[4] = 0;
		}

		if (arg0 == M3GRenderer.KEY_POUND) {
			Keymaps[8] = 0;
		}
		if (arg0 == M3GRenderer.KEY_STAR) {
			Keymaps[9] = 0;
		}

		if (arg0 == M3GRenderer.KEY_NUM0) {
			Keymaps[5] = 0;
		}
		switch (getGameAction(arg0)) {
		case RIGHT:
			Keymaps[1] = 0;
			break;
		case LEFT:
			Keymaps[3] = 0;
			break;
		case UP:
			Keymaps[0] = 0;
			break;
		case DOWN:
			Keymaps[2] = 0;
			break;
		case FIRE:
			Keymaps[4] = 0;
			break;

		}
	}

	void AddFaceFromSector(Sector aSector, int aFace) {

		if (aSector.iCache[aFace] != null) {
			iWorld.addChild(aSector.iCache[aFace]);
			// aSector.iCache[aFace] = null;
			return;
		}
		switch (aFace) {
		case 0:
			aSector.iCache[aFace] = SquareFactory.MakeXY(
					aSector.iStartPoint.iX, aSector.iStartPoint.iY,
					aSector.iStartPoint.iX + aSector.iDimensions.iX,
					aSector.iStartPoint.iY + aSector.iDimensions.iY,
					(aSector.iStartPoint.iZ), aSector.getARGBColor(aFace));
			iWorld.addChild(aSector.iCache[aFace]);

			break;
		// ////////////////////////////////////
		case 3:

			aSector.iCache[aFace] = (SquareFactory.MakeYZ(
					aSector.iStartPoint.iY, aSector.iStartPoint.iZ,
					aSector.iStartPoint.iY + aSector.iDimensions.iY,
					(aSector.iStartPoint.iZ + aSector.iDimensions.iZ),
					aSector.iStartPoint.iX, aSector.getARGBColor(aFace)));
			iWorld.addChild(aSector.iCache[aFace]);
			break;
		// /////////////////////////////
		case 2:
			aSector.iCache[aFace] = SquareFactory.MakeXY(
					aSector.iStartPoint.iX, aSector.iStartPoint.iY,
					aSector.iStartPoint.iX + aSector.iDimensions.iX,
					aSector.iStartPoint.iY + aSector.iDimensions.iY,
					(aSector.iStartPoint.iZ + aSector.iDimensions.iZ),
					aSector.getARGBColor(aFace));
			iWorld.addChild(aSector.iCache[aFace]);

			break;
		// /////////////////////////////////
		case 1:

			aSector.iCache[aFace] = SquareFactory.MakeYZ(
					aSector.iStartPoint.iY, aSector.iStartPoint.iZ,
					aSector.iStartPoint.iY + aSector.iDimensions.iY,
					(aSector.iStartPoint.iZ + aSector.iDimensions.iZ),
					(aSector.iStartPoint.iX + aSector.iDimensions.iX),
					aSector.getARGBColor(aFace));
			iWorld.addChild(aSector.iCache[aFace]);
			break;
		// /////////////////////////////////
		case 4:
			aSector.iCache[aFace] = SquareFactory.MakeXZ(
					aSector.iStartPoint.iX, aSector.iStartPoint.iZ,
					aSector.iStartPoint.iX + aSector.iDimensions.iX,
					(aSector.iStartPoint.iZ + aSector.iDimensions.iZ),
					aSector.iStartPoint.iY, aSector.getARGBColor(aFace));
			iWorld.addChild(aSector.iCache[aFace]);
			break;

		// /////////////////////////////////
		case 5:
			aSector.iCache[aFace] = SquareFactory.MakeXZ(
					aSector.iStartPoint.iX, aSector.iStartPoint.iZ,
					aSector.iStartPoint.iX + aSector.iDimensions.iX,
					(aSector.iStartPoint.iZ + aSector.iDimensions.iZ),
					aSector.iStartPoint.iY + aSector.iDimensions.iY,
					aSector.getARGBColor(aFace));
			iWorld.addChild(aSector.iCache[aFace]);
			break;
		}
		// aSector.iCache[aFace] = null;
		// return face;
	}

	void Translate(float x, float y, float z) {
		// iOrigin.iX += (int) (x);
		// iOrigin.iY += (int) (y);
		// iOrigin.iZ += (int) (z);
		iCamera3D.translate(x, y, z);
	}

	void addFace(Mesh elementAt) {
		iWorld.addChild(elementAt);
	}
}
